<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="标签" prop="tag">
        <el-input v-model="dataForm.tag" placeholder="标签"></el-input>
      </el-form-item>
      <el-form-item label="父标签" prop="pid">
        <!-- <el-input v-model="dataForm.pid" placeholder="父标签"></el-input> -->
        <el-popover :width="400" ref="tagListPopover" placement="bottom-start" trigger="click" popper-class="popover-pop">
          <template v-slot:reference>
            <el-input v-model="dataForm.parentName" :readonly="true" placeholder="选择标签">
              <template v-slot:suffix>
                <el-icon v-if="dataForm.pid !== '0'" @click.stop="tagListTreeSetDefaultHandle()" class="el-input__icon"><circle-close /></el-icon>
              </template> 
              </el-input>
            </template>
          <div class="popover-pop-body"><el-tree :data="tagList" :props="{ label: 'tag', children: 'children' }" node-key="id" ref="tagListTree" :highlight-current="true" :expand-on-click-node="false" accordion @current-change="tagListTreeCurrentChangeHandle"> </el-tree></div>
        </el-popover>
      </el-form-item>
      <el-form-item label="状态" prop="ifValid">
        <el-select v-model="dataForm.ifValid" placeholder="请选择">
          <el-option label="有效" value="0"></el-option>
          <el-option label="无效" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="顺序" prop="tagOrder">
        <el-input v-model="dataForm.tagOrder" placeholder="顺序"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);
import { IObject } from "@/types/interface";

const visible = ref(false);
const dataFormRef = ref();
const tagList = ref([]);
const tagListPopover = ref();
const tagListTree = ref();
const dataForm = reactive({
  id: "",
  pid: "",
  tag: "",
  ifValid: "",
  tagOrder: "",
  remark: "",
  delFlag: "",
  parentName: ""
});

const rules = ref({
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  getTagList().then(() => {
    if (id) {
      getInfo(id);
    } else {
      tagListTreeSetDefaultHandle();
    }
  });
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/crm/crmbasetag/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
  if (dataForm.pid === "0") {
      return tagListTreeSetDefaultHandle();
    }
  tagListTree.value.setCurrentKey(dataForm.pid);
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/crm/crmbasetag", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};
// 上级标签树, 设置默认值
const tagListTreeSetDefaultHandle = () => {
  dataForm.pid = "0";
  dataForm.parentName = "选择标签";
};
// 获取标签列表
const getTagList = () => {
  return baseService.get("/crm/crmbasetag/list").then((res) => {
    if (res.code !== 0) {
      return ElMessage.error(res.msg);
    }
    tagList.value = res.data;
  });
};
// 上级标签树, 选中
const tagListTreeCurrentChangeHandle = (data: IObject) => {
  dataForm.pid = data.id;
  dataForm.parentName = data.tag;
  tagListPopover.value.hide();
};

defineExpose({
  init
});
</script>